package com.example.jacky.android_easeui.demo.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.jacky.android_easeui.R;
import com.example.jacky.android_easeui.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jacky on 2016/10/9.
 * sounbus app-developer
 */

public class ColorChangeActivity extends BaseActivity {
    public static final String TAG = "ColorChangeActivity";
    @Bind(R.id.click)
    Button mClick;
    private ImageView iv1, iv2;
    private Bitmap alterBitmap;
    private Bitmap srcBmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_copy_bitmap);
        ButterKnife.bind(this);
        mClick.setText("图片的颜色变化");
        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        //给第一个imageview默认设置一个位图
        srcBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        iv1.setImageBitmap(srcBmp);
        //创建原图的一个副本。 可修改  创建的是一个空白的图形。
        alterBitmap = Bitmap.createBitmap(srcBmp.getWidth() * 2, srcBmp.getHeight() * 2, srcBmp.getConfig());
    }

    /**
     * 创建原图 bm的一个拷贝。副本
     *
     * @param view
     */
    public void click(View view) {
        //1.准备一个画板  在上面放上准备好的 空白的位图
        Canvas canvas = new Canvas(alterBitmap);
        //2.准备一个画笔
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        //3.画画
        Matrix m = new Matrix();

        ColorMatrix cm = new ColorMatrix();
        cm.set(new float[]{
                0.5f, 0, 0, 0, 0,
                0, 0.8f, 0, 0, 0,
                0, 0, 0.6f, 0, 0,
                0, 0, 0, 1, 0
        });
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(srcBmp, m, paint);
        iv2.setImageBitmap(alterBitmap);//把原图的副本设置到界面上。
    }
}
